package com.sy.service.impl;

import com.sy.dto.MemberDto;
import com.sy.dto.OrdersDto;
import com.sy.dto.SubjectDto;
import com.sy.entity.AdminUser;
import com.sy.entity.Coach;
import com.sy.entity.Member;
import com.sy.entity.Subject;
import com.sy.exception.BusinessException;
import com.sy.mapper.AdminUserMapper;
import com.sy.service.AdminUserService;
import com.sy.vo.AdminVo;
import com.sy.vo.CoachVo;
import com.sy.vo.MemberVo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class AdminUserServiceImpl  extends AbstractBaseServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    @Transactional(readOnly = true)
    public AdminUser findByLogin(AdminUser adminUser) {
        AdminUser loginAdminUser = adminUserMapper.selectByLogin(adminUser);
        return loginAdminUser;
    }

    @Override
    @Transactional(readOnly = true)
    public List<OrdersDto> findAllMemberOrders(Member member) {
        List<OrdersDto> ordersDtos = adminUserMapper.selectAllMemberOrders(member);
        return ordersDtos;
    }

    @Override
    public boolean saveSubject(Subject subject) {
        boolean flag = adminUserMapper.insertSubject(subject);
        return flag;
    }

    @Override
    public boolean dropSubject(Subject subject) {
        boolean flag = adminUserMapper.deleteSubject(subject);
        return flag;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Subject> findSubjectByCoachAndTime(AdminVo adminVo) {
        List<Subject> subjects = adminUserMapper.selectByCoachAndTime(adminVo);
        return subjects;
    }

    @Override
    public boolean saveCoach(Coach coach) {
        boolean flag = adminUserMapper.insertCoach(coach);
        return flag;
    }

    @Override
    public boolean dropCoach(Coach coach) {
        boolean flag = adminUserMapper.deleteCoach(coach);
        return flag;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Member> getAllMembers(MemberVo memberVo) {
        return adminUserMapper.selectAllMembers(memberVo);
    }

    @Override
    @Transactional(readOnly = true)
    public MemberDto getMemberById(Integer id) {
        if (Objects.isNull(id)) {
            throw new BusinessException(HttpStatus.NOT_ACCEPTABLE, "用户异常");
        }
        MemberDto member = adminUserMapper.selectMemberById(id);
        throwIfNull(member, "用户不存在");
        return member;
    }

    @Override
    public Boolean updateMember(MemberDto memberDto) {
        if (Objects.isNull(memberDto.getId())) {
            throw new BusinessException(HttpStatus.NOT_ACCEPTABLE, "用户异常");
        }
        Boolean aBoolean = adminUserMapper.updateMember(memberDto);
        return aBoolean;
    }

    @Override
    public Boolean dropMember(MemberDto memberDto) {
        if (Objects.isNull(memberDto.getId())) {
            throw new BusinessException(HttpStatus.NOT_ACCEPTABLE, "用户异常");
        }
        return adminUserMapper.deleteMemberById(memberDto);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Coach> getAllCoachs(CoachVo cocahVo) {
        return adminUserMapper.selectAllCoachs(cocahVo);
    }

    @Override
    public boolean saveMember(Member member) {
        boolean b = adminUserMapper.insertMember(member);
        return b;
    }

    @Override
    @Transactional(readOnly = true)
    public List<SubjectDto> findBySubjectName(SubjectDto subject) {
        return adminUserMapper.selectBySubjectName(subject);
    }

    @Override
    public List<OrdersDto> findAllOrdersByMemberName(MemberVo memberVo) {
        return adminUserMapper.selectAllOrdersByUserName(memberVo);
    }

    @Override
    public Boolean dropOrder(OrdersDto OrdersDto) {
        return adminUserMapper.deleteOrderById(OrdersDto);
    }
}
